import requests
import simplejson
import json
import base64
#找寻图片中人脸的关键坐标
def find_face(imgpath):
    print("正在查找……")
    http_url = "https://api-cn.faceplusplus.com/facepp/v3/detect"
    data = {
        "api_key": "S8FHDatzj3I3FxhPRPeCRbM6de9HRjkY",
        "api_secret": "dMbebxO3mhw-akCLIdbtTnlpDcss0cam",
        "image_url": imgpath,
        "return_landmark": 1
    }
    files = {"image_file":open(imgpath,"rb")}
    response = requests.post(http_url,data=data,files=files)
    req_con = response.content.decode('utf-8')
    req_dict = json.JSONDecoder().decode(req_con)
    this_json = simplejson.dumps(req_dict)
    this_json2 = simplejson.loads(this_json)
    faces = this_json2['faces']
    list0 = faces[0]
    rectangel = list0['face_rectangle']
    print(rectangel)
    return rectangel


#换脸


def merge_face(image_url1, image_url2, image_url, number):
    ff1 = find_face(image_url1)
    ff2 = find_face(image_url2)
    rectangle1 = str(str(ff1['top']) + "," + str(ff1['left']) + "," + str(ff1['width']) + "," + str(ff1['height']))
    rectangle2 = str(ff2['top']) + "," + str(ff2['left']) + "," + str(ff2['width']) + "," + str(ff2['height'])
    print(rectangle2)
    url_add = "https://api-cn.faceplusplus.com/imagepp/v1/mergeface"
    f1 = open(image_url1, 'rb')
    f1_64 = base64.b64encode(f1.read())
    f1.close()
    f2 = open(image_url2, 'rb')
    f2_64 = base64.b64encode(f2.read())
    f2.close()
    data = {
        "api_key": "S8FHDatzj3I3FxhPRPeCRbM6de9HRjkY",
        "api_secret": "dMbebxO3mhw-akCLIdbtTnlpDcss0cam",
        "template_base64": f1_64,
        "template_rectangle": rectangle1,
        "merge_base64": f2_64,
        "merge_rectangle": rectangle2,
        "merge_rate": number
    }
    response = requests.post(url_add,data=data)
    req_con1 = response.content.decode('utf-8')
    req_dict = json.JSONDecoder().decode(req_con1)
    result = req_dict['result']
    imgdata = base64.b64decode(result)
    file = open(image_url,'wb')
    file.write(imgdata)
    file.close()

image1 = r"face1.jpg"
image2 = r"face2.jpg"
image = r"newface.jpg"

merge_face(image2, image1, image, 100)
